<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '../stores/modules/user'

const router = useRouter()
const userStore = useUserStore()

// 硬编码的用户账号密码
const users = {
  admin: {
    username: 'admin',
    password: 'admin123',
    type: 'admin'
  },
  guest: {
    username: 'guest',
    password: 'guest123',
    type: 'guest'
  }
}

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
}

// 表单引用
const formRef = ref()

// 登录处理
const handleLogin = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      // 验证用户名和密码
      const adminUser = users.admin
      const guestUser = users.guest

      if (loginForm.username === adminUser.username && loginForm.password === adminUser.password) {
        // 管理员登录成功，存储用户信息到Pinia
        userStore.setUserInfo(adminUser.username, adminUser.type as 'admin' | 'guest')
        ElMessage.success('管理员登录成功')
        // 跳转到管理员页面
        router.push('/admin')
      } else if (loginForm.username === guestUser.username && loginForm.password === guestUser.password) {
        // 游客登录成功，存储用户信息到Pinia
        userStore.setUserInfo(guestUser.username, guestUser.type as 'admin' | 'guest')
        ElMessage.success('游客登录成功')
        // 跳转到首页
        router.push('/home')
      } else {
        // 登录失败
        ElMessage.error('用户名或密码错误')
      }
    } else {
      ElMessage.warning('请填写完整的登录信息')
      return false
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">校园路径分析系统</h2>
      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        label-width="0"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>

        <div class="login-tips">
          <p>管理员账号: admin / admin123</p>
          <p>游客账号: guest / guest123</p>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
  font-size: 24px;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  margin-top: 10px;
}

.login-tips {
  margin-top: 20px;
  color: #909399;
  font-size: 13px;
  text-align: center;
}

.login-tips p {
  margin: 5px 0;
}
</style>

